<?php
/**
 * 测试机申请
 * Enter description here ...
 * @author cc
 * apply        测试机申请
 * apply_insert 添加或者修改表单
 * apply_list   测试机申请列表
 * apply_del    删除申请
 * apply_edit   修改申请
 * sign_wait    负责人待签字列表
 * sign_edit    负责人签字
 * sign_over    已签字列表
 * screening_wait  待筛选列表
 * screening_over  已筛选列表
 * screening_edit  筛选
 * audit_wait   待审核列表
 * audit_edit   审核测试机申请
 * audit_over   已审核列表
 * send_wait    待发货列表
 * send_edit    发货
 * send_over    已发货列表
 * look         测试机申请表单查看
 *
 */
namespace Home\Controller;
use Think\Controller;
class DeviceController extends CommonController{
	//private static $
	public function way(){
		session("action","Device");
		session("menu_id",I('get.menu_id'));
		$this->redirect("Public/index");
	}
	
	function apply(){
		$date = date('Y-m-d');
		
		$user_name = session('user_name');
		$name = session('name');
		
		$model = M();
		$sql = "SELECT d.dept_header, d.id FROM oa_sys_user u LEFT JOIN oa_sys_dept d ON u.dept_id = d.id WHERE u.user_name='$user_name'";
		$result = $model->query($sql);
		$dept_header = $result[0]['dept_header'];
		if( $dept_header != $user_name){
			$dept_header_name = M("sys_user")->where("user_name='$dept_header'")->getField("name");
		}else{
			$dept_header_name = $name;
			$data['apply_same'] = '1';
		}
		$data['apply_date'] = $date;
		$data['dept_id'] = $result[0]['id'];
		$data['user_name'] = $user_name;
		$data['dept_header_name'] = $dept_header_name;
		$data['apply_username'] = $name;
		$data['higher'] = $dept_header_name;
		$data['status'] = 0;
		$this->assign('data', $data);
		$this->assign("empty", emptyhtml("list"));
		$this->assign('action', "apply");
		$this->display();
	}
	
	public function apply_insert(){
		if(!IS_POST){$this->error("非法表单！");}
		$time = date('Y-m-d H:i:s');
		
		$data = $_POST;
		$model = M("serial_list");
		
		$dept_id = session("dept_id");
		$serial_data = M("serial_set")->where("dept_id=$dept_id")->find();//查询本区域的测试机授权台数和以使用台数
		if(count($serial_data) == 0){//如果无本区域的测试机授权台数细信息，则联系商务部授权人员
			$this->error("对不起，本部门暂无测试机授权信息，请联系商务部确认！");
		}else{
			if( $data['way'] == '2' ){//提交状态
				$dept_data = M("sys_dept")->where("id=$dept_id")->find();
				if( $dept_data['dept_header'] == session("user_name") ){//如果申请人和同部门负责人相同
//					$file = "../Conf/config.ini";
//					$ini = $this->load_ini($file);
					if( $serial_data['num'] >= $serial_data['number'] ){//当出库的测试机数量超出了授权的测试机台数，则提交给公司负责人审核
						$data['status'] = 2;//待公司负责人审批，由admin选择分类
						$info['user_name'] = "admin";
					}else{//否则到跳过公司负责人审批步骤
						$data['status'] = 4;//待商务部授权装机审批
						$info['user_name'] = "admin";
					}
					//跳过直属销售审核状态
					$data['higher_audit'] = 1;
					$data['higher_sign']  = session("user_name");
					$data['higher_sign_time'] = $time;
					$data['higher_sign_reason'] = "审核通过";
				}else{
					$data['status'] = 1;//待直属销售审核状态
					$info['user_name'] = $dept_data['dept_header'];
				}
			}else{
				$data['status'] = 0;//申请状态
			}
			$data['apply_user_name'] = session('user_name');
			
			$id = I('post.id');
			if($id == ''){
				$success = $model->add($data);
			}else{
				if(!is_id($id)){$this->error("非法参数！");}
				$success = $model->where('id=%d',$id)->save($data);
				if(success){
					if( $data['way'] == '2' ){//保存提交时
						$info['content'] = "客户名称为：  <font color='red'>".$data['client_name']."</font>  由".session("name")."申请的测试机流程于  ".$time." 到您这，请注意查看";
						$this->information("测试机申请流程", $info['content'], $info['user_name']);
					}	
					$this->success("测试机申请表填写成功！", "apply_list");
				}else{
					$this->error("填报测试机申请表失败！");
				}
			}
		}
	}
	
	public function apply_list(){
		$model = M("serial_list");
		$content = I('post.content');
		if(!is_name($content)){ $this->error("客户名称只能是汉字！"); }
		$user_name = session('user_name');
		if($content != ""){ //条件搜索
			$count = $model->where("apply_user_name='$user_name' and client_name like '%$content%'")->count();
		}else{
			$count = $model->where("apply_user_name='$user_name'")->count();
		}
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();
		
		if($content != ""){
			$data = $model->where("apply_user_name='$user_name' and client_name like '%$content%'")->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
		}else{
			$data = $model->where("apply_user_name='$user_name'")->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
		}
		foreach ($data as $key=>$value){
			if($value['type'] == '1'){
				$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
			}elseif($value['type'] == '2'){
				$data[$key]['dev_type'] = "数据库审计系统";
			}
		}
		
		$this->assign("empty", emptyhtml("list"));
		$this->assign("content", $content);
		$this->assign("page",$show);
		$this->assign("data",$data);
		$this->display();
	}
	
	public function delete(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数"); }
		
		$model = M("serial_list");
		$user_name = session('user_name');
		$client_name = $model->where("apply_user_name='$user_name' and id=%d",$id)->getField("client_name");
		$success = $model->where("apply_user_name='$user_name' and id=%d and status=0",$id)->delete();
		if($success){
			$this->systemlog("delete", "测试机申请", "删除测试机申请---".$client_name);
			$this->ajaxReturn("删除测试机申请成功！");
		}else{
			$this->ajaxReturn("删除测试机申请失败！");
		}
	}
	
	public function edit(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数"); }
		
		$model = M("serial_list");
		$user_name = session('user_name');
		$data = $model->where("apply_user_name='$user_name' and id=%d and status=0",$id)->find();
		$this->assign('data', $data);
		$this->assign('action', "apply");
		$this->display('apply');
	}
	
	public function sign_wait(){
		$model = M("serial_list");
		
		$user_name = session('user_name');
		$name = session('name');
		
		$model1 = M();
		$sql = "SELECT d.dept_header, d.id FROM oa_sys_user u LEFT JOIN oa_sys_dept d ON u.dept_id = d.id WHERE u.user_name='$user_name'";
		$result = $model1->query($sql);
		$dept_header = $result[0]['dept_header'];
		if( $dept_header != $user_name){
			$this->no_power();
		}else{
			$user = M("sys_user");
			$content = I('post.content');
			if(!empty($content) && is_space($content)){
				$where = " AND client_name like '%$content%'";//条件查询
			}else{
				if(!empty($content) && !is_space($content)){ $this->error("查询的内容不能有空格！"); }
				$where = "";
			}
			
			$dept_id = $user->where("user_name='$user_name'")->getField('dept_id');
			
			$count = $model->where("dept_id=$dept_id and status=1".$where)->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$data = $model->where("dept_id=$dept_id and status=1".$where)->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
			foreach ($data as $key=>$value){
				if($value['type'] == '1'){
					$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
				}elseif($value['type'] == '2'){
					$data[$key]['dev_type'] = "数据库审计系统";
				}
			}
			
			$this->assign("empty", emptyhtml("list"));
			$this->assign("content", $content);
			$this->assign("page",$show);
			$this->assign("data",$data);
			$this->display();
		}
	}
	
	public function sign_edit(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数"); }
		$user_name = session('user_name');
		$model = M();
		$sql = "SELECT d.dept_header, d.id FROM oa_sys_user u LEFT JOIN oa_sys_dept d ON u.dept_id = d.id WHERE u.user_name='$user_name'";
		$result = $model->query($sql);
		$dept_header = $result[0]['dept_header'];
		if( $dept_header != $user_name){//当该用户不是部门负责人
			echo "<div style='width: 250px;height: 150px;margin: 0 auto; color:red;'>无权限查看</div>";
		}else{
			$sql = "SELECT s.* FROM oa_sys_user u, oa_serial_list s WHERE u.user_name='$user_name' and u.dept_id = s.dept_id and s.id=$id and s.status=1";
			$result = $model->query($sql);
			$data = $result[0];
			$this->assign('data', $data);
			$this->assign('action', "sign");
			$this->display();
		}
	}

	public function sign_over(){
		$model = M("serial_list");
		
		$user_name = session('user_name');
		$name = session('name');
		$dept_id = session("dept_id");
		
		$model1 = M();
		$sql = "SELECT d.dept_header, d.id FROM oa_sys_user u LEFT JOIN oa_sys_dept d ON u.dept_id = d.id WHERE u.user_name='$user_name'";
		$result = $model1->query($sql);
		$dept_header = $result[0]['dept_header'];
		if( $dept_header != $user_name){
			$this->no_power();
		}else{
			$user = M("sys_user");
			
			$content = I('post.content');
			if(!empty($content) && is_space($content)){
				$where = " AND client_name like '%$content%'";//条件查询
			}else{
				if(!empty($content) && !is_space($content)){ $this->error("查询的内容不能有空格！"); }
				$where = "";
			}
			
			$count = $model->where("dept_id=$dept_id and higher_sign='$user_name' and status > 1".$where)->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$data = $model->where("dept_id=$dept_id and higher_sign='$user_name' and status > 1".$where)->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
			foreach ($data as $key=>$value){
				if($value['type'] == '1'){
					$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
				}elseif($value['type'] == '2'){
					$data[$key]['dev_type'] = "数据库审计系统";
				}
			}
			
			$this->assign("empty", emptyhtml("list"));
			$this->assign("content", $content);
			$this->assign("page",$show);
			$this->assign("data",$data);
			$this->display();
		}
	}
	
	public function sign_insert(){
		if(!IS_POST){$this->error("非法表单！");}
		$id = I('post.id');
		if(!is_id($id)){$this->error("非法参数！");}
		
		$time = date('Y-m-d H:i:s');
		
		$post = $_POST;
		$model = M();
		$sql = "select u.name from oa_sys_user u, oa_serial_list s where s.id=$id and s.apply_user_name=u.user_name";
		$data_user = $model->query($sql);
		$username = $data_user[0]['name'];//查询申请人的姓名
		//查询申请人
		$apply_user_name = M("serial_list")->where('id=%d',$id)->getField("apply_user_name");
		
		$dept_id = session("dept_id");
		$serial_data = M("serial_set")->where("dept_id=$dept_id")->find();//查询本区域的测试机授权台数和以使用台数
		if(count($serial_data) == 0){//如果无本区域的测试机授权台数细信息，则联系商务部授权人员
			$this->error("对不起，本部门暂无测试机授权信息，请联系商务部确认！");
		}else{
			$dept_data = M("sys_dept")->where("id=$dept_id")->find();
			if( $dept_data['dept_header'] == session("user_name") ){//如果申请人和同部门负责人相同
				if( $post['way'] == '2' ){//提交状态
					if( $post['higher_audit'] == 1 ){//如果审核通过
						if( $serial_data['num'] >= $serial_data['number'] ){//当出库的测试机数量超出了授权的测试机台数，则提交给公司负责人审核
							$data['status'] = 2;//待公司负责人审批，有朱江峰转送
							//消息通知朱江峰转发
							$info['user_name'] = "zhujiangfeng";
							$info['content'] = "客户名称为：  <font color='red'>".$post['client_name']."</font>  由".$username."申请的测试机流程于  ".$time." 到您这，请注意查看";
						}else{//否则到跳过公司负责人审批步骤
							$data['status'] = 4;//待商务部授权装机审批
							//消息通知发货人员
							$info['user_name'] = "zouyuan";
							$info['content'] = "客户名称为：  <font color='red'>".$post['client_name']."</font>  由".$username."申请的测试机流程于  ".$time." 到您这，请注意查看";
						}
						//消息通知申请人，该流程的动态
						$info1['user_name'] = $apply_user_name;
						$info1['content'] = "您申请的客户名称为：  <font color='red'>".$post['client_name']."</font>  的测试机流程由".session("name")."于  ".$time." 审核<font color='#BCEE68'>通过</font>";
					}else{
						$data['status'] = 10;//表示流程结束
						$info['user_name'] = $apply_user_name;
						$info['content'] = "您申请的客户名称为：  <font color='red'>".$post['client_name']."</font>  的测试机流程由".session("name")."于  ".$time." 审核<font color='red'>否决</font>，否决原因是：".$post['higher_sign_reason'];
					}
				}
				$data['higher_audit'] = $post['higher_audit'];
				$data['higher_sign']  = session("user_name");
				$data['higher_sign_time'] = $time;
				$data['higher_sign_reason'] = $post['higher_sign_reason'];
				
				$success = M("serial_list")->where('id=%d and dept_id=%d',$id,$dept_id)->save($data);
				if(success){
					if( $post['way'] == '2' ){//提交状态
						if( $post['higher_audit'] == 1 ){//如果审核通过
							$this->information("测试机申请", $info1['content'], $info1['user_name']);
						}
						$this->information("测试机申请", $info['content'], $info['user_name']);
					}
					$this->success("测试机申请表审核成功！", "sign_wait");
				}else{
					$this->error("测试机申请表审核失败！");
				}
			}else{
				$this->error("对不起，您无权审批该流程！");
			}
		}
	}
	
	public function screening_wait(){
		$model = M("serial_list");
		
		$user_name = session('user_name');
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			$content = I('post.content');
			if(!empty($content) && is_space($content)){
				$where = " AND client_name like '%$content%'";//条件查询
			}else{
				if(!empty($content) && !is_space($content)){ $this->error("查询的内容不能有空格！"); }
				$where = "";
			}
			
			$count = $model->where("status=2 and higher_audit=1".$where)->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$data = $model->where("status=2 and higher_audit=1".$where)->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
			foreach ($data as $key=>$value){
				if($value['type'] == '1'){
					$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
				}elseif($value['type'] == '2'){
					$data[$key]['dev_type'] = "数据库审计系统";
				}
			}
			
			$this->assign("empty", emptyhtml("list"));
			$this->assign("content", $content);
			$this->assign("page",$show);
			$this->assign("data",$data);
			$this->display();
		}
	}
	
	public function screening_edit(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数"); }
		$user_name = session('user_name');
		$model = M();
		$sql = "SELECT d.dept_header, d.id FROM oa_sys_user u LEFT JOIN oa_sys_dept d ON u.dept_id = d.id WHERE u.user_name='$user_name'";
		$result = $model->query($sql);
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			$data = M("serial_list")->where("id=$id and status=2 and higher_audit=1")->find();
			$this->assign('data', $data);
			$this->assign('action', "screening");
			$this->display();
		}
	}
	
	public function screening_insert(){
		if(!IS_POST){$this->error("非法表单！");}
		$id = I('post.id');
		if(!is_id($id)){$this->error("非法参数！");}
		
		$time = date('Y-m-d H:i:s');
		$user_name = session('user_name');
		
		$post = $_POST;
		$model = M();
		$sql = "select u.name from oa_sys_user u, oa_serial_list s where s.id=$id and s.apply_user_name=u.user_name";
		$data_user = $model->query($sql);
		$username = $data_user[0]['name'];//查询申请人的姓名
		//查询申请人
		$apply_user_name = M("serial_list")->where('id=%d',$id)->getField("apply_user_name");
		
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			if( $post['way'] == '2' ){//提交状态
					$data['status'] = 3;//待公司审批
					//消息通知发货人员
					$info['user_name'] = $post['screening_user_name'];
					$info['content'] = "客户名称为：  <font color='red'>".$post['client_name']."</font>  由  ".$username."  申请的测试机流程于  ".$time." 到您这，请注意查看";
					
					//消息通知申请人，该流程的动态
					$info1['user_name'] = $apply_user_name;
					$info1['content'] = "您申请的客户名称为：  <font color='red'>".$post['client_name']."</font>  的测试机流程由  ".session('name')."  于  ".$time." 转交给公司负责人审批！";
			}
			$data['screening_user_name'] = $post['screening_user_name'];
			$success = M("serial_list")->where('id=%d and status=2',$id)->save($data);
			if(success){
				if( $post['way'] == '2' ){//提交状态
					$this->information("测试机申请", $info1['content'], $info1['user_name']);
					$this->information("测试机申请", $info['content'], $info['user_name']);
				}
				$this->success("测试机申请表审核成功！", "audit_wait");
			}else{
				$this->error("测试机申请表审核失败！");
			}
		}
	}
	
	public function screening_over(){
		$model = M("serial_list");
		
		$user_name = session('user_name');
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			$content = I('post.content');
			if(!empty($content) && is_space($content)){
				$where = " AND client_name like '%$content%'";//条件查询
			}else{
				if(!empty($content) && !is_space($content)){ $this->error("查询的内容不能有空格！"); }
				$where = "";
			}
			
			$count = $model->where("status=3 and higher_audit=1".$where)->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$data = $model->where("status=3 and higher_audit=1".$where)->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
			foreach ($data as $key=>$value){
				if($value['type'] == '1'){
					$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
				}elseif($value['type'] == '2'){
					$data[$key]['dev_type'] = "数据库审计系统";
				}
			}
			
			$this->assign("empty", emptyhtml("list"));
			$this->assign("content", $content);
			$this->assign("page",$show);
			$this->assign("data",$data);
			$this->display();
		}
	}
	
	public function audit_wait(){
		$model = M("serial_list");
		
		$user_name = session('user_name');
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			$content = I('post.content');
			if(!empty($content) && is_space($content)){
				$where = " AND client_name like '%$content%'";//条件查询
			}else{
				if(!empty($content) && !is_space($content)){ $this->error("查询的内容不能有空格！"); }
				$where = "";
			}
			
			$count = $model->where("status=3 and higher_audit=1".$where)->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$data = $model->where("status=3 and higher_audit=1 and screening_user_name='$user_name'".$where)->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
			foreach ($data as $key=>$value){
				if($value['type'] == '1'){
					$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
				}elseif($value['type'] == '2'){
					$data[$key]['dev_type'] = "数据库审计系统";
				}
			}
			
			$this->assign("empty", emptyhtml("list"));
			$this->assign("content", $content);
			$this->assign("page",$show);
			$this->assign("data",$data);
			$this->display();
		}
	}
	
	public function audit_edit(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数"); }
		$user_name = session('user_name');
		$model = M();
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			$data = M("serial_list")->where("id=$id and status=3 and higher_audit=1")->find();
			$this->assign('data', $data);
			$this->assign('action', "audit");
			$this->display();
		}
	}
	public function audit_insert(){
		if(!IS_POST){$this->error("非法表单！");}
		$id = I('post.id');
		if(!is_id($id)){$this->error("非法参数！");}
		
		$time = date('Y-m-d H:i:s');
		$user_name = session('user_name');
		
		$post = $_POST;
		$model = M();
		$sql = "select u.name from oa_sys_user u, oa_serial_list s where s.id=$id and s.apply_user_name=u.user_name";
		$data_user = $model->query($sql);
		$username = $data_user[0]['name'];//查询申请人的姓名
		//查询申请人
		$apply_user_name = M("serial_list")->where('id=%d',$id)->getField("apply_user_name");
		
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			if( $post['way'] == '2' ){//提交状态
				if( $post['audit'] == 1 ){//如果审核通过
					$data['status'] = 4;//待商务部授权装机审批
					//消息通知发货人员
					$info['user_name'] = "zouyuan";
					$info['content'] = "客户名称为：  <font color='red'>".$post['client_name']."</font>  由  ".$username."  申请的测试机流程于  ".$time." 到您这，请注意查看";
					
					//消息通知申请人，该流程的动态
					$info1['user_name'] = $apply_user_name;
					$info1['content'] = "您申请的客户名称为：  <font color='red'>".$post['client_name']."</font>  的测试机流程由  ".session('name')."  于  ".$time." 审核<font color='#BCEE68'>通过</font>";
				}else{
					$data['status'] = 10;//表示流程结束
					$info['user_name'] = $apply_user_name;
					$info['content'] = "您申请的客户名称为：  <font color='red'>".$post['client_name']."</font>  的测试机流程由  ".session("name")."  于  ".$time." 审核<font color='red'>否决</font>，否决原因是：".$post['audit_reason'];
				}
			}
			$data['audit'] = $post['audit'];
			$data['audit_author']  = $user_name;
			$data['audit_time'] = $time;
			$data['audit_reason'] = $post['audit_reason'];
			
			$success = M("serial_list")->where('id=%d and status=3',$id)->save($data);
			if(success){
				if( $post['way'] == '2' ){//提交状态
					if( $post['higher_audit'] == 1 ){//如果审核通过
						$this->information("测试机申请", $info1['content'], $info1['user_name']);
					}
					$this->information("测试机申请", $info['content'], $info['user_name']);
				}
				$this->success("测试机申请表审核成功！", "audit_wait");
			}else{
				$this->error("测试机申请表审核失败！");
			}
		}
	}

	public function audit_over(){
		$model = M("serial_list");
		
		$user_name = session('user_name');
		
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			$content = I('post.content');
			if(!empty($content) && is_space($content)){
				$where = " AND client_name like '%$content%'";//条件查询
			}else{
				if(!empty($content) && !is_space($content)){ $this->error("查询的内容不能有空格！"); }
				$where = "";
			}
			
			
			$count = $model->where("(audit = 0 or audit = 1) and status > 3".$where)->count();
			$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$data  = $model->where("(audit = 0 or audit = 1) and status > 3".$where)->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
			foreach ($data as $key=>$value){
				if($value['type'] == '1'){
					$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
				}elseif($value['type'] == '2'){
					$data[$key]['dev_type'] = "数据库审计系统";
				}
			}
			
			$this->assign("empty", emptyhtml("list"));
			$this->assign("content", $content);
			$this->assign("page",$show);
			$this->assign("data",$data);
			$this->display();
		}
	}
	
	public function send_wait(){
		$model = M("serial_list");
		
		$user_name = session('user_name');
		
		$content = I('post.content');
		if(!empty($content) && is_space($content)){
			$where = " AND client_name like '%$content%'";//条件查询
		}else{
			if(!empty($content) && !is_space($content)){ $this->error("查询的内容不能有空格！"); }
			$where = "";
		}
		
		$count = $model->where("status=4".$where)->count();
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();
		
		$data = $model->where("status=4".$where)->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
		foreach ($data as $key=>$value){
			if($value['type'] == '1'){
				$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
			}elseif($value['type'] == '2'){
				$data[$key]['dev_type'] = "数据库审计系统";
			}
		}
		$this->assign("empty", emptyhtml("list"));
		$this->assign("content", $content);
		$this->assign("page",$show);
		$this->assign("data",$data);
		$this->display();
	}
	
	public function send_edit(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数"); }
		$user_name = session('user_name');
		$model = M();
		$author = array("admin");
		if( !in_array($user_name, $author)){
			echo emptyhtml("html");
		}else{
			$data = M("serial_list")->where("id=$id and status=4 and higher_audit=1")->find();
			$this->assign('data', $data);
			$this->assign('action', "audit");
			$this->display();
		}
	}
	
	public function send_insert(){
		if(!IS_POST){$this->error("非法表单！");}
		$id = I('post.id');
		if(!is_id($id)){$this->error("非法参数！");}
		
		$time = date('Y-m-d H:i:s');
		$user_name = session('user_name');
		
		$post = $_POST;
		$model = M();
		$sql = "select u.name from oa_sys_user u, oa_serial_list s where s.id=$id and s.apply_user_name=u.user_name";
		$data_user = $model->query($sql);
		$username = $data_user[0]['name'];//查询申请人的姓名
		//查询申请人
		$apply_user_name = M("serial_list")->where('id=%d',$id)->getField("apply_user_name");
		
		$author = array("admin");
		if( !in_array($user_name, $author)){
			$this->no_power();
		}else{
			if( $post['way'] == '2' ){//提交状态
				//消息通知申请人，该流程的动态
				$data['status'] = 5;
				$info['user_name'] = $apply_user_name;
				$info['content'] = "您申请的客户名称为：  <font color='red'>".$post['client_name']."</font>  的测试机流程由  ".session('name')."  于  ".$time." 发货！";
			}
			$data['serial_number'] = $post['serial_number'];
			$data['send_user_name']  = $user_name;
			$data['send_time'] = $time;
			
			$success = M("serial_list")->where('id=%d and status=4',$id)->save($data);
			if(success){
				if( $post['way'] == '2' ){//提交状态
					$this->information("测试机申请", $info['content'], $info['user_name']);
				}
				$this->success("测试机申请表审核成功！", "audit_wait");
			}else{
				$this->error("测试机申请表审核失败！");
			}
		}
	}
	

	public function send_over(){
		$model = M("serial_list");
		
		$count = $model->where("status = 5")->count();
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
		$show = $page->show();
		
		$data = $model->where("status = 5")->order("apply_date desc")->limit($page->firstRow.','.$page->listRows)->select();
		foreach ($data as $key=>$value){
			if($value['type'] == '1'){
				$data[$key]['dev_type'] = "统一安全管理与综合审计系统";
			}elseif($value['type'] == '2'){
				$data[$key]['dev_type'] = "数据库审计系统";
			}
		}
		
		$this->assign("page",$show);
		$this->assign("data",$data);
		$this->display();
	}
	
	public function view(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数"); }
		
		$model = M("serial_list");
		$data = $model->where("id=%d",$id)->find();
		if( !empty($data) ){
			$data["higher_sign"] = M("sys_user")->where("user_name='%s'", $data["higher_sign"])->getField("name");
		}
		$this->assign("data",$data);
		$this->display("look");
	}
	
	public function gettype(){//根据设备类型选择产品型号
		$type = I("get.type");
		if($type != 1 && $type != 2){$this->error("非法参数！");}
		if($type == 1){
			$data = M()->query("select id,utm_name as name from oa_sys_utm");
		}elseif($type == 2){
			$data = M()->query("select id,dbx_name as name from oa_sys_dbx");
		}
		echo json_encode($data);
	}
	
	public function gettypes(){//根据设备类型选择产品型号
		$type = I("get.type");
		$model = I("get.model");
		if( ( $type != 1 && $type != 2) || !is_id($model) ){$this->error("非法参数！");}
		if($type == 1){
			$name = M("sys_utm")->where("id = %d", $model)->getField("utm_name");
		}elseif($type == 2){
			$name = M("sys_dbx")->where("id = %d", $model)->getField("dbx_name");
		}
		$this->ajaxReturn($name);
	}
	
	public function set(){//各区域测试机授权的台数和已出库的台数
		$model = M("serial_set");
		$data = $model->select();
		foreach ( $data as $val ){
			$number[] = $val['number'];
		}

		$this->assign("number", $number);
		$this->display();
	}
    
	public function setting(){
		$regional = I('post.regional');
		$number = I('post.number');
		for( $i = 0; $i<count( $regional ); $i++ ){
			if( !is_id($number) ){ 
				$this->error("第".($i+1)."行值只能为大于1的正整数！"); 
			}else{
				$data['number'] = implode(",", $number);
				M("serial_set")->where("id=".($i+1) )->setField("number", $number[$i]);
			}
		}
		$this->success("修改成功！", 'set');
	}
	
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>